package com.yanqu.road.server.cmd.activity.ghost;

import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.GhostActivityProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.cross.battle.ghost.GhostActivity;
import com.yanqu.road.server.manager.cross.battle.ghost.GhostMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;

@Cmd(code = CrossProtocol.C2_GHOST_ACTIVITY_GET_DISPERSE_REWARD, desc = "领取送财奖励")
public class CrossGhostGetDisperseRewardCmd extends CrossCommand {

    int activityId;
    long serverId;
    long userId;
    int type;
    long userResetTime;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        GhostActivityProto.CrossGetDisperseGoldRewardReqMsg reqMsg = GhostActivityProto.CrossGetDisperseGoldRewardReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        serverId = reqMsg.getServerId();
        userId = packet.getPlayerId();
        type = reqMsg.getType();
        userResetTime = reqMsg.getUserResetTime();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        GhostActivity activity = GhostMgr.getActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_GHOST_ACTIVITY_NO_OPEN;
            return;
        }

        if (!activity.isGhostDataResetFinish()) {
            ret = GameErrorCode.E_GHOST_ACTIVITY_NO_OPEN;
            return;
        }

        ret = activity.getDisperseReward(type, serverId, userId, userResetTime);
    }

    @Override
    public void sendData() {
        if (ret != 0) {
            GhostActivityProto.GetDisperseGoldRewardRespMsg.Builder builder = GhostActivityProto.GetDisperseGoldRewardRespMsg.newBuilder();
            builder.setRet(ret);
            MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(ClientProtocol.U_GHOST_GET_DISPERSE_REWARD, builder));
        }
    }
}
